package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 把Excel表格中的数据存储到数据库
     * @param data  通过POI工具类获取的预约对象集合
     */
    @Override
    public void add(List<OrderSetting> data) {
        if (data.size() > 0 && data != null) {
            for (OrderSetting orderSetting : data) {
                int count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    orderSettingDao.updaetorderSetting(orderSetting);
                } else {
                    orderSettingDao.addorderSetting(orderSetting);
                }
            }
        }
    }

    /**
     *显示出预约设置的信息
     * @param date  指定哪天的日期
     * @return  JSON格式的日期数据
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> mapList= new ArrayList<>();
        if (orderSettings!=null&&orderSettings.size()>0){
            for (OrderSetting orderSetting : orderSettings) {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("date",orderSetting.getOrderDate().getDate());
                hashMap.put("number",orderSetting.getNumber());
                hashMap.put("reservations",orderSetting.getReservations());
                mapList.add(hashMap);
            }
        }
        return mapList;
    }

    /**
     * 为指定的日期设置预约的人数
     * 如果数据库有信息     就执行预约修改操作
     * 如果数据库没有信息    就执行预约的添加操作
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        int count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        System.out.println(orderSetting);
        if (count>0){
            orderSettingDao.updaetorderSetting(orderSetting);
        }else {
            orderSettingDao.addorderSetting(orderSetting);
        }
    }
}
